package com.acme.bankapp.domain.bank;

import com.acme.bankapp.domain.bank.exception.NotEnoughFundsException;
import com.acme.bankapp.domain.bank.exception.OverDraftLimitExceededException;

/**
 * Checking account.
 * 
 */
@SuppressWarnings("serial")
public class CheckingAccount extends AbstractAccount {
	private final double overdraft;

	/**
	 * Creates checking account.
	 * 
	 * @param amount
	 *            initial amount of funds
	 * @param overdraft
	 *            overdraft available to the client
	 */
	public CheckingAccount(final double amount, final double overdraft) {
		super(amount);
		this.overdraft = overdraft;
	}

	/**
	 * {@inheritDoc} It's possible for balance to go below zero, up to minus
	 * overdraft limit.
	 */
	@Override
	public void withdraw(double amount) throws NotEnoughFundsException {
		if (amount <= maximumAmountToWithdraw()) {
			if (amount <= balance) {
				super.withdraw(amount);
			} else {
				balance -= amount;
			}
		} else {
			throw new OverDraftLimitExceededException(amount
					- maximumAmountToWithdraw());
		}
	}

	/**
	 * @inheritDoc
	 */
	@Override
	public double maximumAmountToWithdraw() {
		return balance + overdraft;
	}

	/**
	 * @inheritDoc
	 */
	@Override
	public String toString() {
		return String.format(
				"Checking account, balance: $%.2f, overdraft limit: $%.2f",
				getBalance(), overdraft);
	}
}
